package com.ete.lib.upload.hardware

import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.NetworkUtils
import com.ete.lib.upload.UploadManager
import com.ete.lib.upload.bean.HardwareEntry
import com.ete.lib.upload.net.api.UploadService
import com.ete.lib.upload.net.bean.BaseResponse
import com.ete.lib.upload.net.exception.ExceptionTranslate
import com.ete.lib.upload.net.http.RetrofitClient
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response

/**
 * Author:LiXian
 * <p>
 * Desc:上报硬件信息
 * Version:1.0
 * date 2024/1/12 13:07
 */
object UploadHardwareUtil {

    /**
     * 上报硬件信息
     *
     * @param mHardwareEntries 硬件信息
     * @param mCallback 上报回调
     */
    @JvmStatic
    @JvmOverloads
    fun uploadHardwareInfo(mHardwareEntries: List<HardwareEntry>, mCallback: IUploadCallback? = null) {
        mCallback?.onStart()
        val mResponseCallback = object : Callback<BaseResponse<String>> {
            override fun onResponse(call: Call<BaseResponse<String>>, response: Response<BaseResponse<String>>) {
                if (!response.isSuccessful) {
                    LogUtils.d(UploadManager.TAG, "上报硬件信息，请求服务器错误: ${response.message()}")
                    mCallback?.onError("请求服务器错误: ${response.message()}")
                    return
                }
                val result = response.body()
                if (result == null) {
                    LogUtils.d(UploadManager.TAG, "上报硬件信息，服务器响应为空")
                    mCallback?.onError("服务器响应为空")
                    return
                }
                val isSuccess = result.succ
                if (isSuccess == null || !isSuccess) {
                    LogUtils.d(UploadManager.TAG, ("上报硬件信息，" + (result.msg ?: "请求服务器错误")))
                    mCallback?.onError("上报硬件信息失败：${result.msg}")
                    return
                }
                val isUploadSuccess = result.data
                if (isUploadSuccess == null) {
                    LogUtils.d(UploadManager.TAG, "上报硬件信息失败")
                    mCallback?.onError("上报硬件信息失败：${result.msg}")
                    return
                }
                LogUtils.d(UploadManager.TAG, "上报硬件信息成功")
                mCallback?.onFinish()
            }

            override fun onFailure(call: Call<BaseResponse<String>>, t: Throwable) {
                t.printStackTrace()
                val message = ExceptionTranslate.translateException(t).message
                LogUtils.d(UploadManager.TAG, "上报硬件信息错误message:[$message]")
                mCallback?.onError(message)
            }
        }
        RetrofitClient.createService(UploadService::class.java)
            .uploadHardwareInfo(mHardwareEntries).enqueue(mResponseCallback)
    }

    /**
     * 获取网络类型
     */
    @JvmStatic
    fun getNetworkType(): Int {
        // 0:没有网络 1:蜂窝网络 2: WIFI网络
        return when (NetworkUtils.getNetworkType()) {
            NetworkUtils.NetworkType.NETWORK_WIFI -> 2
            NetworkUtils.NetworkType.NETWORK_2G,
            NetworkUtils.NetworkType.NETWORK_3G,
            NetworkUtils.NetworkType.NETWORK_4G,
            NetworkUtils.NetworkType.NETWORK_5G -> 1
            else -> 0
        }
    }
}